<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BView
 *
 * @author jianfeng
 */
class BView {

    public $rows = array();
    protected $model = null;

    //put your code here
    public function setModel($model) {
        if (is_string($model))
            $this->model = new $model();
        if ($model instanceof CActiveRecord)
            $this->model = $model;
    }

    public function getModel($data = NULL) {
        return $this->model;
    }

    public function load($data = NULL) {
        $model = $this->getModel();
        if ($model instanceof CActiveRecord) {
            if (is_array($data)) {
                if (isset($data['sql'])) {//find by sql
                    $connection = $model->getDbConnection();
                    if ($connection == NULL)
                        $connection = Blyn::app()->getActiveApp()->getDbAdapter()->getDbConnection();

                    $sql = $data['sql'];
                    //$condition = isset($data['condition']) ? $data['condition'] : NULL;
                    $params = isset($data['params']) ? $data['params'] : array();
                    $command = $connection->createCommand($sql);
                    $results = $command->queryAll();

                    //$aRows = $model->findAllBySql($sql, $params);
                    foreach ($results as $aRow) {
                        $row = new BRow();
                        $row->setModel($model);
                        $row->attributes = $aRow;
                        $this->rows[$row->getId()] = $row;
                    }
                    $this->isLoad = TRUE;
                } else {
                    $attributes = $data;
                    if (isset($data['attributes'])) {//find by attributes
                        $attributes = $data['attributes'];
                    }
                    $condition = isset($data['condition']) ? $data['condition'] : '';
                    $params = isset($data['params']) ? $data['params'] : array();
                    $aRows = $model->findAllByAttributes($attributes, $condition, $params);
                    foreach ($aRows as $aRow) {
                        $row = new BRow();
                        $row->setModel($model);
                        $row->attributes = $aRow->attributes;
                        $this->rows[$row->getId()] = $row;
                    }
                    $this->isLoad = TRUE;
                }
            }

            //todo-list:maybe need a searchable table for search for row
            //when set attributes in row
            if ($data instanceof BRow) {
                $attributes = $data->attributes;
                $aRows = $model->findAllByAttributes($attributes);
                foreach ($aRows as $aRow) {
                    $row = new BRow();
                    $row->setModel($model);
                    $this->rows[$row->getId()] = $row;
                }
                $this->isLoad = TRUE;
            }
        }
    }

}
